package com.yimixian.app.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.ta.utdid2.android.utils.StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.R;
import com.yimixian.app.cart.CartDetailActivity;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.CartDeliverGoodsGroup;
import com.yimixian.app.order.ConfirmOrderActivity;
import com.yimixian.app.ui.AutoMoreTextLinearlayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhilian-2 on 2016/2/23.
 */
public class CartDialogRecyclerAdapter extends RecyclerView.Adapter<CartDialogRecyclerAdapter.ViewHolder>{

    Context context;
    Dialog mDialog;
    List<CartDeliverGoodsGroup> delieverGoodsGroup = new ArrayList<>();
    long groupId;

    public CartDialogRecyclerAdapter(Context context,
                                     List<CartDeliverGoodsGroup> delieverGoodsGroup, Dialog mDialog) {
        this.context = context;
        this.mDialog = mDialog;
        this.delieverGoodsGroup = delieverGoodsGroup;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.cart_dialog_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CartDeliverGoodsGroup mCartDeliverGoodsGroup = delieverGoodsGroup.get(position);
        if (mCartDeliverGoodsGroup == null) return;
        String groupTitle = mCartDeliverGoodsGroup.groupTitle;
        holder.deliveryText.setText(StringUtils.isEmpty(groupTitle) ? "" : groupTitle);
        holder.mAutoMoreTextLinearlayout.setInitData(mCartDeliverGoodsGroup.totalDesc.desc, "#666666", 13);
        holder.settlementBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toConfirm(position);
            }
        });
    }

    public void toConfirm(int position){
        if (delieverGoodsGroup == null) return;
        CartDeliverGoodsGroup mCartDeliverGoodsGroup = delieverGoodsGroup.get(position);
        if (mCartDeliverGoodsGroup == null) return;
        groupId = mCartDeliverGoodsGroup.groupId;
        if (groupId <= 0) return;
        MobclickAgent.onEvent(context, StatConstants.HOME_CART_SELECT_FINISH_CLICK);
        mDialog.dismiss();
        ((CartDetailActivity) context).startActivityForResult(
                new Intent(context, ConfirmOrderActivity.class).putExtra("group_id",
                        groupId),
                Constants.CONFIRM_ORDER_REQUEST_CODE);
    }


    @Override
    public int getItemCount() {
        return delieverGoodsGroup.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView deliveryText;
        public AutoMoreTextLinearlayout mAutoMoreTextLinearlayout;
        public Button settlementBtn;
        public ViewHolder(View itemView) {
            super(itemView);
            deliveryText = (TextView) itemView.findViewById(R.id.delivery_text);
            settlementBtn = (Button) itemView.findViewById(R.id.settlement_btn);
            mAutoMoreTextLinearlayout = (AutoMoreTextLinearlayout)
                    itemView.findViewById(R.id.auto_loadmore_listview);
        }
    }
}
